package at.fhj.swd.pse.data;

import java.util.ArrayList;
import java.util.List;

import at.fhj.swd.pse.entities.User;

/**
 * 
 * @author Florian Maderbacher
 *
 */

public class UserDAOMock implements UserDAO {

	List<User> m_lstUsers;
	
	public UserDAOMock(){
		m_lstUsers = new ArrayList<User>();
	}
	
	@Override
	public User insert(User entity) {
		m_lstUsers.add(entity);
		return entity;
	}

	@Override
	public User get(int id) {
		for(int i =0 ; i < m_lstUsers.size(); i++){
			if(m_lstUsers.get(i).getId() == id){
				return m_lstUsers.get(i);
			}
		}
		return null;
	}

	@Override
	public List<User> getAll() {
		return m_lstUsers;
	}

	@Override
	public void remove(User entity) {
		m_lstUsers.remove(entity);
	}

	@Override
	public User update(User entity) {
		for(int i =0 ; i < m_lstUsers.size(); i++){
			if(m_lstUsers.get(i).getId() == entity.getId()){
				return m_lstUsers.set(i, entity);
			}
		}
		return null;
	}

	@Override
	public User getByLogin(String username, String password) {
		if(username == null || password == null)
			return null;
		
		for(int i =0 ; i < m_lstUsers.size(); i++){
			User u = m_lstUsers.get(i);
			if(username.equals(u.getUsername()) 
					&& password.equals(u.getPassword())){
				return m_lstUsers.get(i);
			}
		}
		return null;
	}

}
